package pay.alipay.utils;


import pay.alipay.sign.RSA;
import pay.config.AlipayConfig;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class SignUtils {
    /**
     * 支付宝签名
     * @param array
     * @return
     */
    public synchronized static String signAllString(String [] array){
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < array.length; i++) {
            if(i==(array.length-1)){
                sb.append(array[i]);
            }else{
                sb.append(array[i]+"&");
            }
        }
        String sign = "";
        try {
            sign = URLEncoder.encode(RSA.sign(sb.toString(), AlipayConfig.private_key, "utf-8"), "utf-8");      //private_key私钥
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        sb.append("&sign=\""+sign+"\"&");
        sb.append("sign_type=\"RSA\"");
        System.out.println("签名："+sign);
        return sb.toString();
    }
}
